package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.AddressDao;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private AddressDao addressDao;

    //预约设置
    @Override
    public Result submitOrder(OrderInfo orderInfo) throws Exception {

        //根据orderDate判断预约的日期是否可以预约  //跟改格式
        Date orderDate = DateUtils.parseString2Date(orderInfo.getOrderDate());
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(orderDate);

        //判断预约日期
        if(orderSetting == null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //可以预约
        //判断预约名额是否已满
        if(orderSetting.getNumber()==orderSetting.getReservations()){
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //可以预约,预约名额未满
        //判断该用户是否是会员,不会会员自动注册为会员==>根据电话号码判断
        String telephone = orderInfo.getTelephone();
        Member member = memberDao.findByTelephone(telephone);
        Integer setmealId = orderInfo.getSetmealId();

        //获得该机构地址id==>根据经纬度
        Address address = addressDao.findByLngAndLat(orderInfo.getAddressValue());

        //已经为会员
        if(member != null){
            //判断该成员是否在当天已经预约了改套餐==>根据OrderDate,telephone,套餐,addressIdo
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(orderDate);
            order.setSetmealId(setmealId);

            List<Order> orderCondition = orderDao.findByCondition(order);

            if(orderCondition != null && orderCondition.size() > 0){

                for (Order order1 : orderCondition) {

                    Integer addressId = addressDao.findByOrderId(order1.getId());
                    if(addressId.equals(address.getId())){
                        //判断该会员已经预约

                        return new Result(false,MessageConstant.HAS_ORDERED);
                    }
                }

            }

        }

        //不是会员,注册会员
        if(member == null){
            //获得姓名
            String name = orderInfo.getName();
            String idCard = orderInfo.getIdCard();
            String sex = orderInfo.getSex();

            member = new Member();
            member.setName(name);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setSex(sex);
            member.setRegTime(orderDate);

            memberDao.add(member);
        }

        //没有预约过
        //添加预约
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setSetmealId(setmealId);
        order.setOrderType(orderInfo.getOrderType());
        order.setOrderStatus(Order.ORDERSTATUS_NO);

        orderDao.add(order);

        //给预约中间加一
        addressDao.addOrderIdAndAddressId(address.getId(),order.getId());

        //给orderSetting中的已预约加一
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.addOneReservation(orderSetting);

        //预约成功
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    //查询预约的相关信息
    @Override
    public Map findById(Integer id) throws Exception {

        Map map = orderDao.findById(id);

        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
